//http.ts
import axios from 'axios';
import { baseUrl, mode } from '@/config';
import { message, Spin } from 'antd';
// 设置请求头和请求路径
axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8';
const instance = axios.create({
  baseURL: mode.IS_DEV ? '/' : baseUrl,
  timeout: 10000,
  headers: {},
});
instance.interceptors.request.use(
  config => {
    return config;
  },
  error => {
    return error;
  },
);
// 响应拦截
instance.interceptors.response.use(({ data }): any => {
  if (data.code === 200) {
    return Promise.resolve(data);
  } else if (data.code !== 200 && data.code !== 2001) {
    message.error(data?.err);
    return Promise.reject(data.err);
  } else if (data.code === 2001) {
    // 登录失效
    message.error(data?.err);
    return Promise.reject('请重新登录');
  }
});

export default instance;
